package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;


    @RequiresPermissions(value = "企业管理")
    @RequestMapping("/list")
    public String findAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {

        //List<Company> list = companyService.findAll();
        //request.setAttribute("list",list);

        PageInfo info = companyService.findPageByHelper(page,size);
        request.setAttribute("page",info);
        return "company/company-list";  ///WEB-INF/pages/ company/company-list    .jsp
    }

    //跳转到Add页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/company/company-add";
    }

    //保存企业（保存或者更新）
    @RequestMapping("/edit")
    public String edit(Company company){
        if (StringUtils.isEmpty(company.getId())){
            companyService.save(company);
        }else {
            companyService.update(company);
        }

        return "redirect:/company/list.do";
    }

    //企业修改
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Company company = companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-update";
    }

    //删除企业
    @RequestMapping("/delete")
    public String delete(String id){
        companyService.delete(id);
        return "redirect:/company/list.do";
    }

}
